package com.huopaolan.yohuoshop.Mvp.View.Activity;

import android.content.ContentValues;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.huopaolan.yohuoshop.Dao.Service.DBService;
import com.huopaolan.yohuoshop.R;
import com.huopaolan.yohuoshop.Sp.SpUtils;

import static android.view.View.*;

public class UserNameUpdateActivity extends AppCompatActivity implements OnClickListener {
    private ImageView user_back;
    private TextView user_saves;
    private EditText update_user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_user_name_update);
        initView();
    }

    private void initView() {
        user_back = findViewById(R.id.update_back);
        user_saves = findViewById(R.id.user_save);
        update_user = findViewById(R.id.user_update_name);
        user_saves.setOnClickListener(this);
        user_back.setOnClickListener(this);
    }

    @Override
    protected void onStart() {
        Window window = getWindow();
        window.setStatusBarColor(Color.BLACK);
        super.onStart();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.update_back:
                finish();
                break;
            case R.id.user_save:
                String spReadUser = SpUtils.getInstance().SpReadUser();
                String username = update_user.getText().toString();
                Toast.makeText(this, "" + username, Toast.LENGTH_SHORT).show();
                ContentValues values = new ContentValues();
                values.put("nick_name", username);
                int user = DBService.getDbService().update("User", values, "user_id=?", new String[]{spReadUser});
                Toast.makeText(this, "" + user, Toast.LENGTH_SHORT).show();
                if (user > 0) {
                    finish();
                }
                DBService.getDbService().close();
                break;
        }
    }
}
